package view.controller;

import javafx.fxml.FXML;
import javafx.scene.Cursor;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import utils.Connect;
import utils.User;
import utils.receiveRunnable;
import view.MainWindow;
import view.Register;

import java.io.IOException;
/**
 * @author
 */
public class LoginController {
    @FXML
    public Label register;
    @FXML
    public Button login;
    @FXML
    public TextField account;
    @FXML
    public TextField password;

    @FXML
    public void loginView(MouseEvent event) throws IOException {
        String account1 = account.getText();
        String password1 = password.getText();

        String info;
        Connect connect = Connect.getConnect();
        connect.startConnect();
        User user = connect.login(account1, password1);
        System.out.println(user);
        if (user!=null) {
            Stage stage = (Stage) login.getScene().getWindow();
            stage.close();
            Thread thread=new Thread(new receiveRunnable(Connect.getConnect().getSocket()));
            thread.start();
            new MainWindow(user);
        } else {
            info = "账号或密码错误!";
            Alert alert = new Alert(Alert.AlertType.INFORMATION, info, new ButtonType("确认", ButtonBar.ButtonData.YES));
            alert.setHeaderText(null);
            alert.setTitle("提示");
            alert.show();
        }
    }

    @FXML
    public void setMousePoint() {
        login.setCursor(Cursor.HAND);
        register.setCursor(Cursor.HAND);
    }

    public void login(KeyEvent event) throws IOException {
        if (event.getCode() == KeyCode.ENTER) {
            this.loginView(null);
        }
        //this.alert(new ActionEvent());
    }


    public void onRegisterClicked(MouseEvent mouseEvent) throws IOException {
        new Register();
    }
}
